package com.gugee.common.web.system.promise.validate;

import com.gugee.common.web.system.promise.repository.PermissionLimitRepository;
import com.gugee.common.web.system.user.repository.UserRoleRepository;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PropertyLimit extends Validate {

    @Autowired
    UserRoleRepository userRoleRepository;

    @Autowired
    PermissionLimitRepository permissionLimitRepository;


    @Override
    public Boolean validate(long roleId,Object ... params) {
        val permissionLimitOptional = permissionLimitRepository.findByPermissionIdAndProductIdAndAllow(super.permissionId, roleId, params[0].toString());

        if(permissionLimitOptional.isPresent()){
            return true;
        }
        val permissionLimit = permissionLimitRepository.findTop1ByPermissionIdAndProductId(super.permissionId, roleId);
        super.tips = permissionLimit.getTips();
        return false;
    }

}
